<script setup lang="ts">
import { ref } from 'vue'
import { mobileRules, passwordRules } from '@/utils/rules'
import { userLogin } from '@/services/user'
import { showFailToast, showLoadingToast, showSuccessToast } from 'vant'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'

const mobile = ref('')
const password = ref('')
const isShow = ref(false)
const store = useUserStore()
const route = useRoute()
const router = useRouter()
const onSubmit = async () => {
  const t = showLoadingToast({
    message: '登录中',
    duration: 0,
    loadingType: 'spinner'
  })
  try {
    const res = await userLogin(mobile.value, password.value)
    t.close()
    store.setUser({ ...res.data, mobile: mobile.value })
    showSuccessToast('登陆成功')
    router.replace((route.query.returnUrl as string) || '/home')
  } catch (error) {
    t.close()
    showFailToast('登陆失败')
  }
}
</script>

<template>
  <div class="login-page">
    <van-nav-bar title="· IT资讯" fixed :border="false">
      <template #left>
        <div class="logo">
          <ic-icon name="logo"></ic-icon>
        </div>
      </template>
    </van-nav-bar>
    <div class="login-form">
      <div class="form-head">
        <h3>欢迎登陆</h3>
      </div>
      <van-form autocomplete="off" @submit="onSubmit" ref="form">
        <van-field
          name="mobile"
          v-model="mobile"
          :rules="mobileRules"
          placeholder="请输入手机号"
          type="tel"
        ></van-field>
        <van-field
          v-model="password"
          :rules="passwordRules"
          placeholder="请输入密码"
          :type="isShow ? 'text' : 'password'"
        >
          <template #button>
            <span class="close-or-open" v-if="password" @click="password = ''">
              <ic-icon name="login-close"></ic-icon>
            </span>
            <span
              class="close-or-open"
              @click="isShow = !isShow"
              style="margin-left: 12px"
            >
              <ic-icon
                :name="`login-preview-${isShow ? 'open' : 'close'}`"
              ></ic-icon>
            </span>
          </template>
        </van-field>
        <div class="cell">
          <!-- native-type	原生 button 标签的 type 属性	默认button -->
          <van-button native-type="submit" block type="primary">
            登 录
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
:deep() {
  .van-nav-bar {
    &__left {
      padding-left: 14px;
    }
    &__title {
      width: 67px;
      height: 26px;
      line-height: 26px;
      color: var(--ic-color1);
      font-weight: 400;
      font-size: 18px;
      margin-left: 100px;
    }
  }
}
::v-deep(.van-field) {
  padding-left: 0;
  font-size: 18px;
  font-weight: 500;
}
::v-deep(.van-cell:after) {
  left: 0;
}
::v-deep(.van-button) {
  width: 327px;
  height: 48px;
  font-size: 16px;
}
.close-or-open {
  display: inline-block;
  width: 20px;
  height: 20px;
}
.logo {
  width: 80px;
  height: 26px;
}
.login-form {
  padding: 0 24px;
  margin-top: 98px;
  h3 {
    margin-bottom: 60px;
    width: 96px;
    height: 34px;
    line-height: 34px;
    font-size: 24px;
    font-weight: 600;
  }
  .cell {
    margin-top: 28px;
  }
}
</style>
